//
//  TestDetailSecondViewController.m
//  Koizora
//
//  Created by dlios on 15-6-11.
//  Copyright (c) 2015年 CupCat. All rights reserved.
//

#import "TestDetailSecondViewController.h"
#import "TestDetailThirdViewController.h"
#import "NetworkHandle.h"
#import "TestDetailSecondModel.h"
#import <UIImageView+WebCache.h>
#import "TestDetailTableViewCell.h"

@interface TestDetailSecondViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *secondArr;
@property (nonatomic, strong) TestDetailSecondModel *secondModel;
@property (nonatomic, strong) NSMutableArray *secondDetailArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *thirdDetailArr;

@end

@implementation TestDetailSecondViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self getData];
}

- (void)createTestTwoDetailView
{
    self.coverImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height / 4)];
//    _coverImgView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_coverImgView];
    
    CGFloat f = [[self class] heightWithText:[_secondDetailArr[0] objectForKey:@"questionTitle"]];
    self.questionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _coverImgView.frame.size.height + _coverImgView.frame.origin.y + 10, self.view.bounds.size.width, f)];
    _questionTitleLabel.numberOfLines = 0;
//    _questionTitleLabel.backgroundColor = [UIColor cyanColor];
    [self.view addSubview: _questionTitleLabel];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _questionTitleLabel.frame.size.height + _questionTitleLabel.frame.origin.y + 10, self.view.bounds.size.width, self.view.bounds.size.height - _questionTitleLabel.frame.size.height - _questionTitleLabel.frame.origin.y) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil){
        cell = [[TestDetailTableViewCell
                 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.optionContent.text = [[[_secondDetailArr [0]objectForKey:@"options"] objectAtIndex:indexPath.row] objectForKey:@"optionContent"];
    cell.optionNum.text = [[[_secondDetailArr [0]objectForKey:@"options"] objectAtIndex:indexPath.row] objectForKey:@"optionNum"];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_secondDetailArr [0]objectForKey:@"options"]count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestDetailThirdViewController *thirdView = [[TestDetailThirdViewController alloc] init];
    thirdView.testID = _testID;
    thirdView.thirdDic = _thirdDetailArr[indexPath.row];
    [self.navigationController pushViewController:thirdView animated:YES];
}
+(CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 1000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}


-(void)getData
{
    NSString *qq = [NSString stringWithFormat:@"http://star.zhuoyouapp.com/v1/questionnaireServer/getTestById.action?testId=%@", _testID];
    [NetworkHandle getDataWithURL:qq completion:^(id result) {
        self.secondArr = [NSMutableArray array];
        self.secondDetailArr = [NSMutableArray array];
        _secondDetailArr = [[result objectForKey:@"testEntity"] objectForKey:@"questions"];
        TestDetailSecondModel *secondModel = [[TestDetailSecondModel alloc] initWithDic:[result objectForKey:@"testEntity"]];
        [self.secondArr addObject:secondModel];
        NSLog(@"%@",secondModel);
        
        self.thirdDetailArr = [NSMutableArray array];
        _thirdDetailArr = [[result objectForKey:@"testEntity"] objectForKey:@"answers"];
        
        
        [self createTestTwoDetailView];
        
        [_coverImgView sd_setImageWithURL:[NSURL URLWithString:[[_secondArr firstObject] coverImg]]placeholderImage:nil];
        
       _questionTitleLabel.text = [_secondDetailArr[0] objectForKey:@"questionTitle"];
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
